package com.wushijia.config;

import java.util.Locale;
import org.springframework.context.annotation.Configuration;
import org.springframework.lang.Nullable;
import org.springframework.web.servlet.View;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.ViewResolverRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

/**
 * description:spring boot2推荐使用继承WebMvcConfigurationSupport，或实现WebMvcConfigurer；spring boot 1.x使用继承WebMvcConfigurerAdapter
 * 默认目录：src/main/resources,可通过配置文件修改：spring.resources.static-locations=classpath:/static/
 *
 * @author yang
 * @date 2018/11/27 19:59
 */
@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

  @Override
  protected void addResourceHandlers(ResourceHandlerRegistry registry) {
    //拦截/static请求，并指向到/static/文件夹下
    if (registry.hasMappingForPattern("/static/**")) {
      registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
    }
    super.addResourceHandlers(registry);
  }

  @Override
  public void addInterceptors(InterceptorRegistry registry) {
    registry.addInterceptor(new HandlerInterceptorAdapter() {
    }).addPathPatterns("/**");
  }

  @Override
  protected void configureViewResolvers(ViewResolverRegistry registry) {
    registry.jsp("/views/",".jsp");
  }
}
